// pages/sponsorManage/sponsorMDetail.js
var $=require('../util/commit.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param:{},
    showPageStatus:true,
    sid:'',
    title:'',
    pic:'',
    mobile:'',
    content:'',
    default:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    let that = this;
    let param = JSON.parse(options.data);
    if(param.Type=='Add'){
        that.setData({
          showPageStatus:true,
        })
    }else if(param.Type=='Eidt'){
   
      that.setData({
        showPageStatus: false,
        param,
      });
      that.loadData();
    }
   
  },
  loadData:function(e){
    let that=this;
    $.POST({
      url:'caSSD',
      data:{
        sid:that.data.param.sid,
      }
    },function(e){
      that.setData({
        sid:e.data.sid,
        title: e.data.title,
        pic: e.data.pic,
        default: e.data.default,
        mobile: e.data.mobile,
        content: e.data.content,
      })
    },function(e){})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // updateUrl上传头像
  updateUrl:function(e){
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0];
      
        if (src) {
          // 将图片参数传递给插件
          $.openWin({
            url: '../cropper/cropper',
            data: {
              CutPageType: 'sponsorMDetail',
              url: src,
            }
          });
        }

      },
      fail(res) {

      }
    })
  },
  // TitleChange
  TitleChange:function(e){
    let that=this;
    that.setData({
      title: e.detail.value
    })
  },
  // MobileChange
  MobileChange:function(e){
    let that = this;
    that.setData({
      mobile: e.detail.value
    })
  },
  // ContentChange
  ContentChange:function(e){
    let that = this;
    that.setData({
      content: e.detail.value
    })
  },
  // delBtn 删除
  delBtn:function(e){
    let that=this;
    $.POST({
      url:'caDS',
      data:{
        sid: e.currentTarget.dataset.sid
      }
    },function(e){
      wx.showToast({
        title:e.msg,
      });
      wx.navigateBack();
    },function(e){});
  },
  // 保存SaveBtn
  SaveBtn:function(e){
    let that=this;
    if (that.data.title){
      if (that.data.pic){
        if (that.data.mobile) {
          if (that.data.content) {
            let dataParam='';
            if(e.currentTarget.dataset.type=='add'){
              dataParam = {
          
                title: that.data.title,
                pic: that.data.pic,
                mobile: that.data.mobile,
                content: that.data.content
              }
            }else{
              dataParam = {
                sid: e.currentTarget.dataset.sid,
                title: that.data.title,
                pic: that.data.pic,
                mobile: that.data.mobile,
                content: that.data.content
              }
            }
            $.POST({
              url: 'caASS',
              data: dataParam
            }, function (e) {
              wx.showToast({
                title: e.msg,
                icon: 'none',
              });
              wx.navigateBack();
            }, function (e) { })
          } else {
            wx.showToast({
              title: '请填写简介',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: '请填写手机号',
            icon: 'none'
          })
        }
      }else{
        wx.showToast({
          title: '请上传头像',
          icon: 'none'
        })
      }
    }else{
      wx.showToast({
        title: '请填写名称',
        icon:'none'
      })
    }

  }
})